package com.gistone.commis.controller;

import com.gistone.commis.database.GetBySqlMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("property")
public class PropertyController {

	@Autowired
	public GetBySqlMapper getBySqlMapper;
	
	String community_id = null;
	
	@RequestMapping("select")
	public void select( HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		//获取分页有关参数
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		int page = pageNumber == 0 ? 1 : (pageNumber / pageSize) + 1;
		
		//从session中获取相关id
/*		HttpSession session = request.getSession();
		JSONArray sesJson = JSONArray.fromObject(session.getAttribute("userInfo"));
		for (int i = 0; i < sesJson.size(); i++) {
			JSONObject ses_id = sesJson.getJSONObject(i);
			community_id = ses_id.getString("community_id");
			System.out.println("资产列表查询id:community_id=" + community_id);
			}*/
		//查询所有的数据sql
		String all_sql="select * from tb_property where community_id="+1;
		//分页查询sql
		String   limit_sql="select * from tb_property where community_id limit"+pageNumber+","+pageSize;
		//数据库条数
		String count_sql= "select count(*) from tb_property where community_id = "+1;
		
		int count = getBySqlMapper.findrows(count_sql);
		
		//获取资产列表
		List<Map> property_list = getBySqlMapper.findRecords(all_sql);
		System.out.println(property_list.size());
		if (property_list.size()>0) {
			JSONArray pro_jsa = new JSONArray();
			for (int i = 0; i < property_list.size(); i++) {
				Map pro_map = property_list.get(i);
				JSONObject pro_jsobj= new JSONObject();
				//资产编号
				pro_jsobj.put("zcbh", "".equals(pro_map.get("property_id")) || pro_map.get("property_id")==null ? "":pro_map.get("property_id").toString());
				//所属社区
				pro_jsobj.put("community_id", "".equals(pro_map.get("community_id")) || pro_map.get("community_id")==null ? "":pro_map.get("community_id").toString());
				//所属科室
				pro_jsobj.put("depart_id", "".equals(pro_map.get("depart_id")) || pro_map.get("depart_id")==null ? "":pro_map.get("depart_id").toString());
				//填报人员
				pro_jsobj.put("user_id", "".equals(pro_map.get("user_id")) || pro_map.get("user_id")==null ? "":pro_map.get("user_id").toString());
				//实物编号
				pro_jsobj.put("property_code", "".equals(pro_map.get("property_code")) || pro_map.get("property_code")==null ? "":pro_map.get("property_code").toString());
				//资产名称
				pro_jsobj.put("zcmc", "".equals(pro_map.get("property_name")) || pro_map.get("property_name")==null ? "":pro_map.get("property_name").toString());
				//资产类别
				pro_jsobj.put("type_id", "".equals(pro_map.get("type_id")) || pro_map.get("type_id")==null ? "":pro_map.get("type_id").toString());
				//供应商
				pro_jsobj.put("gys", "".equals(pro_map.get("supplier")) || pro_map.get("supplier")==null ? "":pro_map.get("supplier").toString());
				//品牌
				pro_jsobj.put("pp", "".equals(pro_map.get("property_brand")) || pro_map.get("property_brand")==null ? "":pro_map.get("property_brand").toString());
				//规格型号
				pro_jsobj.put("xh", "".equals(pro_map.get("specification_type")) || pro_map.get("specification_type")==null ? "":pro_map.get("specification_type").toString());
				//使用情况
				pro_jsobj.put("syqk", "".equals(pro_map.get("use_status")) || pro_map.get("use_status")==null ? "":pro_map.get("use_status").toString());
				//存放地点
				pro_jsobj.put("cfdz", "".equals(pro_map.get("property_add")) || pro_map.get("property_add")==null ? "":pro_map.get("property_add").toString());
				//配置单位
				pro_jsobj.put("pzdw", "".equals(pro_map.get("configuration_unit")) || pro_map.get("configuration_unit")==null ? "":pro_map.get("configuration_unit").toString());
				//资产价值
				pro_jsobj.put("zcjz", "".equals(pro_map.get("property_worth")) || pro_map.get("property_worth")==null ? "":pro_map.get("property_worth").toString());
				//备注
				pro_jsobj.put("bz", "".equals(pro_map.get("remarks")) || pro_map.get("remarks")==null ? "":pro_map.get("remarks").toString());
				//配置时间
				pro_jsobj.put("pzsj", "".equals(pro_map.get("deploy_time")) || pro_map.get("deploy_time")==null ? "":pro_map.get("deploy_time").toString());
				//填报时间
				pro_jsobj.put("register_time", "".equals(pro_map.get("register_time")) || pro_map.get("register_time")==null ? "":pro_map.get("register_time").toString());
				//数量
				pro_jsobj.put("count", "".equals(pro_map.get("count")) || pro_map.get("count")==null ? "":pro_map.get("count").toString());
				
				pro_jsa.add(pro_jsobj);
			}
			JSONObject pro_jsobj = new JSONObject();
			pro_jsobj.put("rows",pro_jsa );
			pro_jsobj.put("total", count);
			pro_jsobj.put("page", page);
			response.getWriter().write(pro_jsobj.toString());
		}else{
			JSONObject pro_jsobj = new JSONObject();
			pro_jsobj.put("error", "没有这条记录！");
			response.getWriter().write(pro_jsobj.toString());
		}
		
	}
	
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException{
		 
		String pro_id=request.getParameter("del_id");
		String sql= "DELETE FROM tb_property WHERE property_id = " + pro_id;
		
		int del_count = getBySqlMapper.delete(sql);
		response.getWriter().write(del_count);
	}
}
